<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;

use View;
use Session;
use App\Model\Menu;

class MenuController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'menu','status'=>'menu_status'];
        $this->setModelObj(new Menu,$option);
    }

    /**
	 * 后台菜单列表
	 */
	public function listData(Request $request,$pid=0)
	{
		$menu_parents = Menu::where('menu_parent_id',$pid)->orderBy('menu_sort','asc')->get();
        $menulist = $this->getMenuStr($menu_parents);
        $menu_list_json = json_encode($menulist);
		return View::make('admin.menu.menulist', ['menulist'=>$menu_list_json]);
	}
	
	protected function getMenuStr($menulist,$state=1){
		if(count($menulist)){
			foreach ($menulist as $k => $menu){
				$info[$k]['text'] = '<span title="'.$menu->menu_sort.'">'.$menu->menu_name.'</span>';
				
				$del    = "<i class='fa fa-trash-o' onclick=layListDel(this,'/admin/menu/destroy','".$menu->menu_id."','reload')></i>";
				if($menu->menu_status){
					$updown = "<i class='fa fa-level-down' onclick=layChangeStatus('/admin/menu/status/".$menu->menu_id."/0','隐藏')></i>";
				}else{
					$updown = "<i class='fa fa-level-up' onclick=layChangeStatus('/admin/menu/status/".$menu->menu_id."/1','显示')></i>";
				}
				$edit   = "<i class='fa fa-edit' onclick=layOpenView('/admin/menu/editview/".$menu->menu_id."','90%','90%','菜单编辑')></i>";
				
				$info[$k]['tags'] = [$del,$updown,$edit];
				
				$childlist = Menu::where('menu_parent_id', $menu->menu_id)->orderBy('menu_sort','asc')->get();
				$info[$k]['nodes'] = $this->getMenuStr($childlist,$menu->menu_parent_id);
			}
		}else{
            $info = $state ? '' : [];
		}
		return $info;
	}
	
	/**
	 * 后台菜单添加页面
	 */
	public function addDataView()
	{
		$menulist = Menu::where('menu_parent_id',0)->orderBy('menu_sort','asc')->get();
		return View::make('admin.menu.menuadd',['menulist'=>$menulist]);
	}
	
	/**
	 * 后台菜单添加保存
	 */
	public function toAddData(Requests\MenuRequest $request)
	{
		$data = $request->except(['_token','_method']);
		$data['menu_status'] = 1;
		$data['menu_add_time'] = time();
		$data['menu_add_ip'] = $request->getClientIp();
		$data['menu_creator'] = Session::get('adminuser')['user_id'];
		$re = Menu::insert($data);
        $jsonData = $re ? $this->getJsonData(2,'增加成功') : $this->getJsonData(0,'增加失败');
		return response()->json($jsonData);
	}
	
	/**
	 * 后台菜单编辑页面
	 */
	public function updateDataView($menuid)
	{
		$menu_info = Menu::find($menuid);
		$menulist = Menu::where('menu_parent_id',0)->orderBy('menu_sort','asc')->get();
		return View::make('admin.menu.menuedit', ['menuinfo'=>$menu_info,'menulist'=>$menulist]);
	}
	
	/**
	 * 后台菜单编辑保存
	 */
	public function toUpdateData(Requests\MenuRequest $request, $menuid)
	{
		$data = $request->except(['_token','_method']);
		$re = Menu::where('menu_id',$menuid)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
		return response()->json($jsonData);
	}

    /**
     * 判断数据是否可删除
     */
    protected function enableToDel($ids)
    {
        $exit_child = Menu::whereIn('menu_parent_id',$ids)->first();
        return ($exit_child ? false : true);
    }
}